<template>
  <div class="login">
    <div class="login-container">
      <h1 class="login-title">教室管理登录页面</h1>
      <div class="form-group">
        <label for="username">用户名</label>
        <input type="text" id="username" placeholder="请输入用户名" v-model="username">
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" placeholder="请输入密码" v-model="password">
      </div>
      <div class="form-group">
        <label for="mailbox">邮箱</label>
        <input type="text" id="mailbox" placeholder="请输入邮箱" v-model="email">
      </div>
      <div class="Captcha">
        <label for="Captcha">验证码</label>
        <input type="text" id="Captcha" placeholder="请输入验证码" v-model="captcha">
        <span>{{ Captcha }}</span>
        <p @click="handoff()">换一个</p>
      </div>
      <button class="login-button" @click="login">登录</button>
      <p @click="enroll()">注册账号</p>

    </div>
  </div>
</template>

<script>
import { } from '../../utils/https.js'
export default {
  data() {
    return {
      Captcha: '',
      username: '',
      password: '',
      email: '',
      captcha: '',
    }
  },
  methods: {
    //登录获取本地存储数据进行对比
    login() {
      const obj = JSON.parse( localStorage.getItem('Account'))
      if(this.username === obj.username && this.password === obj.password && this.email === obj.email ){
        if(this.captcha === this.Captcha){
          localStorage.setItem('succeed', true)
          this.$router.push('/about/home')
          
        }else{
          alert('验证码错误')
        }
      }else{
        alert('用户名,密码或邮箱错误')
      }
    },
    //验证码随机数
    handoff() {
      this.Captcha = '';
      for (let i = 0; i < 4; i++) {
        this.Captcha += Math.floor(Math.random() * 10);
      }

    },
    //注册页面跳转
    enroll() {
      this.$router.push('/Enroll')
    }
  },
  components: {

  },
  created() {
    //开始显示验证码
    this.handoff();
  },
  watch: {
    //监听验证码的变化防止验证码问题
    'Captcha': {
      immediate: true,
      handler(newVal) {
        this.Captcha = newVal;
        
      }
    },
  }
}
</script>

<style less>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/教室管理登录页图片.png');
  background-size: cover;

  .login-container {
    background-color: rgba(222, 180, 123, 0.3);
    padding: 40px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    width: 360px;

    >h1 {
      font-size: 22px;
    }

    >p {
      float: right;
      font-size: 16px;
      color: rgb(57, 124, 255);
      cursor: pointer;
      font-style: italic;
      text-decoration: underline;
    }

    .login-title {
      text-align: center;
      margin-bottom: 30px;
      color: #333;
    }

    .Captcha {
      margin-bottom: 20px;

      label {
        display: block;
        margin-bottom: 8px;
        color: #666;
      }

      input {
        width: 50%;
        padding: 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        box-sizing: border-box;
        transition: border-color 0.3s ease;

        &:focus {
          outline: none;
          border-color: #007bff;
        }
      }

      span {
        display: inline-block;
        background-color: darkgray;
        height: 40px;
        line-height: 40px;
        font-size: 14px;
        padding: 0px 20px;
        position: relative;
        left: 55px;
      }

      p {
        display: inline-block;
        cursor: pointer;
        position: relative;
        left: 60px;
      }
    }

    .form-group {
      margin-bottom: 20px;

      label {
        display: block;
        margin-bottom: 8px;
        color: #666;
      }

      input {
        width: 100%;
        padding: 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        box-sizing: border-box;
        transition: border-color 0.3s ease;

        &:focus {
          outline: none;
          border-color: #007bff;
        }
      }
    }

    .login-button {
      width: 100%;
      padding: 12px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s ease;
      font-size: 18px;

      &:hover {
        background-color: #0056b3;
      }
    }
  }
}
</style>